#include <QGuiApplication>
#include <QQmlApplicationEngine>

#include <QLocale>
#include <QFile>
#include <QTranslator>
#include <QtWebView/QtWebView>
#include "BaiduLogin.h"
#include "BaiduToken.h"
#include "BaiduPan.h"
#include "localdir.h"

int main(int argc, char *argv[])
{
    QtWebView::initialize();
    QGuiApplication app(argc, argv);

    QTranslator translator;
    const QStringList uiLanguages = QLocale::system().uiLanguages();
    for (const QString &locale : uiLanguages) {
        const QString baseName = "baidunet_" + QLocale(locale).name();
        if (translator.load(":/i18n/" + baseName)) {
            app.installTranslator(&translator);
            break;
        }
    }

    qmlRegisterType<BaiduLogin>("HBaiduLogin", 1, 0, "BaiduLogin");
    qmlRegisterType<BaiduPan>("HBaiduPan", 1, 0, "BaiduPan");
    qmlRegisterType<LocalDir>("HLocalDir", 1, 0, "LocalDir");

    bool islogin = BaiduToken::checkCache();

    QQmlApplicationEngine engine;
    const QUrl url(u"qrc:/baidunet/main.qml"_qs);

    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);

    engine.load(url);

    //判断是否有登录信息
    QObject *qmlobj = engine.rootObjects().at(0);
    if(islogin){
        qmlobj->findChild<QObject *>("loginui")->setProperty("visible", false);
        qmlobj->findChild<QObject *>("fileui")->setProperty("visible", true);
    }else{
        qmlobj->findChild<QObject *>("loginui")->setProperty("visible", true);
        qmlobj->findChild<QObject *>("fileui")->setProperty("visible", false);
    }
    //test code
    //qDebug() << QGuiApplication::styleHints()->mouseDoubleClickInterval() << QGuiApplication::styleHints()->mouseDoubleClickDistance();


    return app.exec();
}
